package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.EventBusTags;
import com.kmproject.haifu.di.component.DaggerHousePublishMainComponent;
import com.kmproject.haifu.mvp.contract.HousePublishMainContract;
import com.kmproject.haifu.mvp.model.entity.beans.HouseMainBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoryBean;
import com.kmproject.haifu.mvp.model.entity.beans.PublishNumCheakBean;
import com.kmproject.haifu.mvp.presenter.HousePublishMainPresenter;
import com.kmproject.haifu.mvp.ui.adapter.HousePublishMainAdapter;
import com.kmproject.haifu.mvp.ui.widget.MoreWindow;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/25/2019 15:54
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HousePublishMainActivity extends BaseActivity<HousePublishMainPresenter> implements HousePublishMainContract.View {
    //
//    @BindView(R.id.HousePublishMain_RecyclerView)
    RecyclerView HousePublishMainRecyclerView;
    PublishNumCheakBean mpublishNumCheakBean;

    MoreWindow mMoreWindow;

    @BindView(R.id.HousePublishMain_cancel_lay)
    LinearLayout HousePublishMainCancelLay;
    @BindView(R.id.house_publish_main_boot)
    LinearLayout housePublishMainBoot;
    MyStoryBean.ResultBean mresultBean;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHousePublishMainComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {

        return R.layout.activity_house_publish_main; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //状态栏透明
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|
                        View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        housePublishMainBoot.postDelayed(new Runnable() {
            @Override
            public void run() {
                showMoreWindow();
            }
        },100);
        mresultBean = getIntent().hasExtra("MyStoryBeanResultBean") ? (MyStoryBean.ResultBean) getIntent().getSerializableExtra("MyStoryBeanResultBean") : null;

        HousePublishMainRecyclerView = findViewById(R.id.HousePublishMain_RecyclerView);
//        ArmsUtils.configRecyclerView(HousePublishMainRecyclerView, new LinearLayoutManager(this));
//        HousePublishMainRecyclerView.setAdapter(new HousePublishMainAdapter(mPresenter.getDatas(), new HousePublishMainAdapter.ItemCallBack() {
//            @Override
//            public void callBack(HouseMainBean.HouseMainItem item) {
//
//
//                if (item.getCode().equals("zf")) {
//                    Intent intent = new Intent(HousePublishMainActivity.this, HousePublishSelectTypeActivity.class);
//                    if (mresultBean != null) {
//                        intent.putExtra("MyStoryBeanResultBean", mresultBean);
//                    }
//                    ArmsUtils.startActivity(intent);
//                } else if (item.getCode().equals("qz")) {
//                    ArmsUtils.startActivity(ReleaseRentSeekingSelectionTypeActivity.class);
//                } else if (item.getCode().equals("eswp")) {
//                    if (mpublishNumCheakBean == null) {
//                        mPresenter.isPublishCount();
//                        showMessage("未获取到配置信息,请重试");
//                        return;
//                    }
//                    if (mpublishNumCheakBean.getSecondCount() == 0) {
//                        new AlertView("", "您的在线二手物品数量已超上限，暂时无法继续发布，您可以升级会员或下架部分二手物品！", "我知道了", new String[]{"去升级会员"}, null, HousePublishMainActivity.this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {
//
//                            @Override
//                            public void onItemClick(Object o, int position) {
//                                if (position == AlertView.CANCELPOSITION) {
//
//                                } else {
//                                    Intent intent = new Intent(HousePublishMainActivity.this, MemberCenterMainActivity.class);
//                                    intent.putExtra("type", "house_updateMember");//从房屋跳转到升级会员界面
//                                    ArmsUtils.startActivityForResult(intent, 1002);
//                                }
//
//                            }
//                        }, ArmsUtils.getColor(HousePublishMainActivity.this, R.color.alert_button_00adfcr), true).show();
//                        return;
//                    }
//                    Intent intent = new Intent(HousePublishMainActivity.this, SecondHandPublicshActivity.class);
//                    ArmsUtils.startActivity(intent);
//                }
//            }
//        }));
    }

    @Subscriber(mode = ThreadMode.MAIN)
    public void reciverLocation(String flag) {
        if (flag.equals(EventBusTags.RELEASE_RENT_SEEKING_SUCCESS)) {
            killMyself();
        }
    }

    @Override
    public String reSetTitle() {
        return "";
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.isPublishCount();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        DefaultAdapter.releaseAllHolder(HousePublishMainRecyclerView);
    }

    @OnClick(R.id.HousePublishMain_cancel_lay)
    public void onClick() {
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        killMyself();
    }

    @Override
    public void isPublishCountSuccess(PublishNumCheakBean publishNumCheakBean) {
        mpublishNumCheakBean = publishNumCheakBean;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        killMyself();
    }

    private void showMoreWindow() {
        if (null == mMoreWindow) {
            mMoreWindow = new MoreWindow(this);
            mMoreWindow.init(housePublishMainBoot, new MoreWindow.ClickCallBack() {
                @Override
                public void callBack(int index) {
                    switch (index)
                    {
                        case 0:
                            Intent intent = new Intent(HousePublishMainActivity.this, HousePublishSelectTypeActivity.class);
                            if (mresultBean != null) {
                                intent.putExtra("MyStoryBeanResultBean", mresultBean);
                            }
                            ArmsUtils.startActivity(intent);
                            break;
                        case 1:
                            ArmsUtils.startActivity(ReleaseRentSeekingSelectionTypeActivity.class);
                            break;
                        case 2:
                            if (mpublishNumCheakBean == null) {
                                mPresenter.isPublishCount();
                                showMessage("未获取到配置信息,请重试");
                                return;
                            }
                            if (mpublishNumCheakBean.getSecondCount() == 0) {
                                new AlertView("", "您的在线二手物品数量已超上限，暂时无法继续发布，您可以升级会员或下架部分二手物品！", "我知道了", new String[]{"去升级会员"}, null, HousePublishMainActivity.this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                                    @Override
                                    public void onItemClick(Object o, int position) {
                                        if (position == AlertView.CANCELPOSITION) {

                                        } else {
                                            Intent intent = new Intent(HousePublishMainActivity.this, MemberCenterMainActivity.class);
                                            intent.putExtra("type", "house_updateMember");//从房屋跳转到升级会员界面
                                            ArmsUtils.startActivityForResult(intent, 1002);
                                        }

                                    }
                                }, ArmsUtils.getColor(HousePublishMainActivity.this, R.color.alert_button_00adfcr), true).show();
                                return;
                            }
                            Intent intent2 = new Intent(HousePublishMainActivity.this, SecondHandPublicshActivity.class);
                            ArmsUtils.startActivity(intent2);
                            break;
                    }
                    mMoreWindow.closeAnimation();

                }
            });

        }

        mMoreWindow.showMoreWindow(housePublishMainBoot);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.no_slide, R.anim.popup_exit);
    }
}
